package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import controller.Sistema;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModificarPrenda extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static final Component IngresarCant = null;
	private JLabel labelStock;
	private JTextField campoStock;
	private JLabel labelMateriales;
	private JScrollPane scrollMateriales;
	private JComboBox<ProductoView> comboPrendas;
	private JList<MaterialView> listaMateriales;
	private JLabel labelMensajeSeleccion;
	private JLabel tituloVentana;
	private JButton botonAceptar;
	private JButton botonCancelar;
	private JOptionPane MensajeConfirmacion;
	private JOptionPane MensajeError;

	public ModificarPrenda() {
		super();
		initGUI();
	}
	
	class MultiColumnCellRendererProductoView extends JPanel implements ListCellRenderer<ProductoView>{
		private static final long serialVersionUID = 1L;
		private final JLabel leftLabel = new JLabel();
		  private final JLabel rightLabel;
		  public MultiColumnCellRendererProductoView(int rightWidth) {
		    super(new BorderLayout());
		    this.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
	
		    leftLabel.setOpaque(false);
		    leftLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
	
		    final Dimension dim = new Dimension(rightWidth, 0);
		    rightLabel = new JLabel() {
				private static final long serialVersionUID = 1L;
	
			@Override public Dimension getPreferredSize() {
		        return dim;
		      }
		    };
		    rightLabel.setOpaque(false);
		    rightLabel.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
		    rightLabel.setForeground(Color.GRAY);
		    rightLabel.setHorizontalAlignment(SwingConstants.RIGHT);
	
		    this.add(leftLabel);
		    this.add(rightLabel, BorderLayout.EAST);
		  }
		  @Override public Component getListCellRendererComponent(JList<? extends ProductoView> list, 
				  ProductoView value, int index, boolean isSelected, boolean cellHasFocus) {
			  
		    ProductoView item = (ProductoView)value;
		    leftLabel.setText(String.valueOf(item.getCodigo()));
		    rightLabel.setText(item.getNombre());
	
		    leftLabel.setFont(list.getFont());
		    rightLabel.setFont(list.getFont());
	
		    if(index<0) {
		      leftLabel.setForeground(list.getForeground());
		      this.setOpaque(false);
		    }else{
		      leftLabel.setForeground(
		          isSelected?list.getSelectionForeground():list.getForeground());
		      this.setBackground(
		          isSelected?list.getSelectionBackground():list.getBackground());
		      this.setOpaque(true);
		    }
		    return this;
		  }
		  
		  @Override public Dimension getPreferredSize() {
		    Dimension d = super.getPreferredSize();
		    return new Dimension(0, d.height);
		  }		  
	}
	
	class CellRendererMaterialView extends JLabel implements ListCellRenderer<MaterialView>{
		
		private static final long serialVersionUID = 1L;
		public CellRendererMaterialView() {
			setOpaque(true);
		  }
		
		public Component getListCellRendererComponent(JList<? extends MaterialView> list, MaterialView value, 
				int index, boolean isSelected, boolean cellHasFocus) {
			  
			this.setText(value.getNombreLista());
		    if (isSelected) {
		        setBackground(list.getSelectionBackground());
		        setForeground(list.getSelectionForeground());
		      } else {
		        setBackground(list.getBackground());
		        setForeground(list.getForeground());
		      }
			return this;
		  }
	}
	
	private void initGUI() {
		try {
			this.setSize(437, 504);
			setLocationRelativeTo(null);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				tituloVentana = new JLabel();
				this.add(tituloVentana);
				tituloVentana.setText("Modificacion de Prenda Sin Temporada");
				tituloVentana.setBounds(12, 12, 294, 27);
				tituloVentana.setFont(new java.awt.Font("Segoe UI",0,16));
			}
			{
				labelMensajeSeleccion = new JLabel();
				getContentPane().add(labelMensajeSeleccion);
				labelMensajeSeleccion.setText("Seleccione la Prenda");
				labelMensajeSeleccion.setBounds(12, 56, 145, 21);
			}
			{
				Vector<ProductoView> productos = new Vector<ProductoView>();
				productos = Sistema.getInstance().obtenerVistaProductosSinTemporada();
				ListCellRenderer<ProductoView> renderer = new MultiColumnCellRendererProductoView(150);
				comboPrendas = new JComboBox<ProductoView>(productos);
				comboPrendas.setRenderer(renderer);
				getContentPane().add(comboPrendas);
				comboPrendas.setBounds(175, 55, 207, 23);
				comboPrendas.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent evt) {
						ProductoView selected = (ProductoView) comboPrendas.getSelectedItem();
						if (selected != null){
							campoStock.setText(String.valueOf(selected.getStock()));		
						}
					}
				});
			}
			{
				labelStock = new JLabel();
				getContentPane().add(labelStock);
				labelStock.setText("Stock");
				labelStock.setBounds(12, 96, 145, 21);
			}
			{
				campoStock = new JTextField();
				getContentPane().add(campoStock);
				campoStock.setBounds(175, 96, 88, 23);
			}
			{
				labelMateriales = new JLabel();
				getContentPane().add(labelMateriales);
				labelMateriales.setText("Nuevos Materiales (mantener CTRL para seleccion multiple):");
				labelMateriales.setBounds(12, 163, 397, 16);
			}
			{
				scrollMateriales = new JScrollPane();
				getContentPane().add(scrollMateriales);
				scrollMateriales.setBounds(24, 199, 335, 188);
				{
					Vector<MaterialView> lista = Sistema.getInstance().obtenerVistaMateriales();
					listaMateriales = new JList<MaterialView>(lista);
					ListCellRenderer<MaterialView> renderer = new CellRendererMaterialView();
					listaMateriales.setCellRenderer(renderer);
					scrollMateriales.setViewportView(listaMateriales);
					listaMateriales.setBounds(24, 158, 335, 192);
				}
			}
			{
				botonCancelar = new JButton();
				getContentPane().add(botonCancelar);
				botonCancelar.setText("Cancelar");
				botonCancelar.setBounds(76, 404, 108, 37);
				botonCancelar.setFont(new java.awt.Font("Segoe UI",0,16));
				botonCancelar.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent evt){
						close();
					}
				});
			}
			{
				botonAceptar = new JButton();
				this.add(botonAceptar);
				botonAceptar.setText("Aceptar");
				botonAceptar.setBounds(226, 404, 95, 37);
				botonAceptar.setFont(new java.awt.Font("Segoe UI",0,16));
				botonAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try{
							Vector<ItemMaterialView> items = new Vector<ItemMaterialView>();
							ProductoView prenda = (ProductoView) comboPrendas.getSelectedItem();
							List<MaterialView> materiales = listaMateriales.getSelectedValuesList();
							for (MaterialView m: materiales){
								String cantidad = JOptionPane.showInputDialog(IngresarCant,"Ingrese una cantidad para el material "+m.getNombre()+" :");
								ItemMaterialView i = new ItemMaterialView(
										Integer.valueOf(cantidad),
										m.getCodigo(),
										prenda.getCodigo()
										);
								items.add(i);
							}
							
							boolean rc = Sistema.getInstance().actualizarPrendaSinTemporada(
									prenda.getCodigo(),
									prenda.getNombre(),
									Integer.valueOf(campoStock.getText()),
									items
									);
							if (rc){
								JOptionPane.showMessageDialog(MensajeConfirmacion, "La prenda se ha modificado satisfactoriamente");
								setVisible(false);
							}else{
								JOptionPane.showMessageDialog(MensajeError, "Error modificando la prenda","Error!",JOptionPane.ERROR_MESSAGE);
							}
						} 
						catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void close(){
		this.dispose();
	}
}